package util;

import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.EditText;
import android.widget.Toast;

import com.beaglebuddy.mp3.MP3;
import com.example.administrator.sdscanner.MainActivity;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/7.
 */

public class Scanner {
    public static final String[] AUDIO_ALL_FORMAT = new String[] {
            ".3ga", ".a52", ".aac", ".ac3", ".adt", ".adts", ".aif", ".aifc",
            ".aiff", ".amr", ".aob", ".ape", ".awb", ".caf", ".dts", ".flac",
            ".it", ".m4a", ".m4b", ".m4p", ".mid", ".mka", ".mlp", ".mod",
            ".mpa", ".mp1", ".mp2", ".mp3", ".mpc", ".mpga", ".oga", ".ogg",
            ".oma", ".opus", ".ra", ".ram", ".rmi", ".s3m", ".spx", ".tta",
            ".voc", ".vqf", ".w64", ".wav", ".wma", ".wv", ".xa", ".xm"
    };

    public static final String[] AUDIO_MUSIC_FORMAT = new String[] {
            ".mp3", ".cda", ".wav", ".mid", ".wma", ".mov", ".vqf"
    };

    // UI接口
    private Handler handler;
    // 扫描的文件格式
    private String[] fileFormat = AUDIO_MUSIC_FORMAT;
    // 扫描结果
    private ArrayList<File> list = new ArrayList<File>();
    // 扫描状态
    private boolean state = false;
    private int threadState = 0;
    // 文件路径
    private String[] sdRoot;
    // 扫描时间
    private long useTime = 0;
    // 定义文件过滤器
    private FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File file) {
            return !file.isHidden();
        }
    };

    public Scanner(String[] sdRoot) {
        this.sdRoot = sdRoot;
    }

    public Scanner(String[] sdRoot, Handler handler) {
        this.sdRoot = sdRoot;
        this.handler = handler;
    }

    public ArrayList<File> getList() {
        return list;
    }

    public String[] getFileFormat() {
        return fileFormat;
    }

    public void setFileFormat(String[] fileFormat) {
        this.fileFormat = fileFormat;
    }

    @JavascriptInterface
    public long getUseTime() {
        return useTime;
    }

    @JavascriptInterface
    public boolean getState() {
        return state;
    }

    @JavascriptInterface
    public void setState(boolean state) {
        this.state = state;
    }

    // 扫描事件
    @JavascriptInterface
    public void scanEvent(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("Scanner", "runing!!!!");
//                if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//                    Log.e("Scanner", "Error! Can't find SD !!!");
//                }
//                // 获得SD卡根目录
//                File sdRoot = Environment.getExternalStorageDirectory();
                long startTime = System.currentTimeMillis();
                for (String str : sdRoot){
                    scanner(new File(str));
                }
                long endTime = System.currentTimeMillis();
                useTime = (endTime - startTime) / 1000;
                state = true;
            }
        }).start();
    }

//    @JavascriptInterface
//    public void scanEvent(){
//        if (sdRoot != null) {
//            for (final String root : sdRoot) {
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
////                    Log.i("Scanner", "runing!!!!");
////                    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
////                        Log.e("Scanner", "Error! Can't find SD !!!");
////                    }
////                    // 获得SD卡根目录
////                    File sdRoot = Environment.getExternalStorageDirectory();
//                        long startTime = System.currentTimeMillis();
//                        scanner(new File(root));
//                        long endTime = System.currentTimeMillis();
//                        synchronized (this){
//                            System.out.println("Thread: " + (endTime - startTime) / 1000);
//                            useTime += (endTime - startTime) / 1000;
//                            threadState++;
//                        }
//                        if (threadState == sdRoot.length){
//                            state = true;
//                        }
//                    }
//                }).start();
//            }
//        }
//    }

    // 扫描文件
    private void scanner(File root){
        File[] files = root.listFiles();
        if (null != files) {
            for (File file : files) {
                if (file.isDirectory()) {
                    scanner(file);
                } else {
                    for (String format : fileFormat) {
                        if (file.getName().endsWith(format) && file.length() >= 600 * 1024) {
                            this.list.add(file);
                            try {
                                Log.i("scanner", "one file!!!");
                                Log.i("Mp3:", file.getAbsolutePath());
                                MP3 mp3 = new MP3(file);
                                if (!mp3.hasErrors()){
                                    Message msg = new Message();
                                    msg.obj = mp3;
                                    handler.sendMessage(msg);
                                }
                            } catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
